home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
hsend.exe
/
HSEND.C
< prev
next >
Wrap
Text File
|
1992-01-11
|
26KB
|
1,066 lines
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dir.h>
#include <dos.h>
#include <time.h>
#include <async.h>
#include <process.h>
#include "hsend.h"
int WAITING,BAUD,COMM,RINGS,SCREENFLAG,LOGFILEFLAG,FLAGFILEFLAG;
int HARDERR_FLAG=0,TIMEOUT=180;
char LOGFILE[61],FLAGFILE[61],PASSWORD[13];
ASYNC *P=NULL;
main(argc,argv)
int argc;
char argv[];
{
ctrlbrk(break_handler);
harderr(hardware_error_handler);
load_data();
if(argc>2){
cprintf("\n\rUsage: hsend <no arguments> Run in host mode\n\r");
cprintf(" hsend <batch file name> Run in guest mode\n\r");
beep(3,400);
exit(1);}
copyright();
if(!init_modem()){
gotoxy(1,21);
textcolor(LIGHTGRAY);
cprintf("\n\rCOMM device did not respond. Check settings with CONFIGUR.\n\r");
beep(3,400);
exit(1);}
clrscr();
textcolor(LIGHTCYAN);
empty_box(1,1,80,20);
empty_box(1,21,80,24);
if(argc==2) guest_mode(argv[2]);
else host_mode();
window(1,1,80,25);
gotoxy(3,22);
textcolor(LIGHTMAGENTA);
cprintf("Initializing modem, please wait...");
init_modem();
a_close(P,0);
textcolor(LIGHTGRAY);
textbackground(BLACK);
clrscr();
exit(0);
}
void host_mode(void)
{
int x,y;
char temp[80];
struct tm *ptr;
time_t lt;
TIMEOUT*=2; /* wait longer if in host mode */
gotoxy(29,1);
cprintf(" H-SEND Host Mode V1.0 ");
window(4,2,77,18);
for(;;){
bottom(&x,&y);
cprintf("Modem is currently OFF-LINE. Press ESC to exit.");
a_printf(P,"AT S0=%d V0\n\r",RINGS);
do{
if(kbhit()) if(getch()==27){ /* ESC key to exit */
clrscr();
return;}
}while(OFFLINE); /* wait here for carrier */
cprintf("\n\rModem is currently ON-LINE");
lt=time(NULL);
ptr=localtime(<);
strcpy(temp,"On-line: ");
strcat(temp,asctime(ptr));
strcat(temp,"\n\r");
report(temp);
top(x,y);
if(password_check()) while(cmd_mode(1));
init_modem();
init_modem(); /* make sure modem is reset */
}
}
int guest_mode(batchfile)
char *batchfile;
{
FILE *fp;
char str[100],temp[80],*buf;
int i,x,y,flag=1;
struct tm *ptr;
time_t lt;
if(FLAGFILEFLAG) remove(FLAGFILE);
strcpy(str,"Executing: ");
strcat(str,batchfile);
gotoxy(39-strlen(batchfile),1);
cprintf(" %s ",str);
window(4,3,77,18);
AGAIN:
if((fp=fopen(batchfile,"r"))==NULL){
report("ERROR- Unable to open batchfile");
return 0;} /* return error condition */
do{
if(fgets(str,100,fp)){
for(i=0;i<strlen(str);i++)
if(str[i]=='\n' || str[i]=='\r') str[i]=NULL;
strupr(str);
textcolor(WHITE);
report("");
report(str);
textcolor(LIGHTCYAN);
flag=interpret_batch_cmd(str);}
}while(!feof(fp) && !flag && !HARDERR_FLAG);
scb(EOC,0,NULL);
lt=time(NULL);
ptr=localtime(<);
strcpy(temp,"Call terminated: ");
strcat(temp,asctime(ptr));
report(temp);
fclose(fp);
if(flag || HARDERR_FLAG){
report("ON-LINE transfer was not sucessful");
x=wherex(); y=wherey();
if((buf=malloc(2*44*9))==NULL) exit(1);
gettext(18,5,62,13,buf);
window(18,5,62,13);
textbackground(LIGHTCYAN);
textcolor(WHITE);
clrscr();
empty_box(2,1,44,9);
gotoxy(11,1);
cprintf(" Data Transfer Failure ");
gotoxy(5,4);
cprintf("The transfer has not been completed.");
beep(1,300);
beep(1,1000);
do{
gotoxy(5,6);
cprintf("Would you like to try again? <Y or N>");
i=getch();
i=toupper(i);
if(i!='Y' && i!='N') beep(1,1000);
}while(i!='Y' && i!='N');
cprintf("%c",i);
textbackground(BLACK);
window(4,3,77,18);
puttext(18,5,62,13,buf);
gotoxy(x,y);
if(i=='Y') goto AGAIN;
window(4,3,77,18);
puttext(18,5,62,13,buf);
return 0;}
else{
report("No error occurred");
if(FLAGFILEFLAG) if((fp=fopen(FLAGFILE,"a"))==NULL){
if((fp=fopen(FLAGFILE,"r"))==NULL) report("Unable to create flagfile");}
return 1;}
}
int cmd_mode(host_flag) /* return 0 at end of sesssion */
{
unsigned char buf[518];
int cmdreq;
for(;;){
if(host_flag){
if(!scb(OKTS,0,NULL)){ /* OK to send */
report("ERROR- OKTS transmission not acknowledged");
return 0;}}
do{ /* receiving command */
if(OFFLINE){
report("DATA CONNECTION LOST");
return 0;}
cmdreq=rcb(0,buf);
}while(!cmdreq);
switch(cmdreq){
case OKTS: return -1;
case SFR: send_file_request(buf,0);
break;
case IFR: create_local_file(buf);
break;
case DF: delete_files(buf,HOST);
break;
case EM: error_message(buf);
break;
case EOC: report("Transmission terminated normally");
return 0;
case RESTART: report("Restarting hsend...");
execlp("hsend.exe",NULL,NULL);
default: report("cmd_mode() default");
if(!host_flag) return -1;} /* switch back to active mode */
} /* endless loop */
}
int passive(void) /* switch to passive mode */
{
int status;
for(;;){
status=cmd_mode(0); /* will be executing received commands while here */
if(!status) return 1; /* outgoing command not acknowledged, returning 1 will
cause guest_mode() to exit with an error condition */
if(status==-1) return 0; /* OKTS received, switch back to send mode */
} /* endless loop */
}
int interpret_batch_cmd(cmd)
char *cmd;
{
char temp[512],from[100],to[100];
int i;
if(strlen(cmd)<4) return 0; /* ignore trash */
if(!strncmp(cmd,"CALL",4)) return(call_remote(cmd));
if(!strncmp(cmd,"REM",3)) return 0; /* remarks */
if(scb(EM,2," ")) if(!rcb(OKTS,temp)) rcb(OKTS,temp); /* send a blank line */
if(scb(EM,strlen(cmd)+1,cmd)) if(!rcb(OKTS,temp)) rcb(OKTS,temp); /* send the command */
/*----------------------- COPY FROM HOST TO GUEST --------------------------------*/
if(!strncmp(cmd,"COPY H",6)){
parse(cmd);
if(!seperate(cmd,from,to,' ')){
report("ERROR- COPY command syntax");
return 1;}
for(i=0;i<strlen(from);i++) /* remove H and L designations */
from[i]=from[i+1];
for(i=0;i<strlen(to);i++)
to[i]=to[i+1];
strcpy(temp,from); /* set up SFR string */
strcat(temp,"|");
strcat(temp,to);
if(!scb(SFR,strlen(temp)+1,temp)){
report("ERROR- Host unable to verify SFR transmission");
return 1;}
else return passive();}
/*--------------------------------------------------------------------------------*/
/*----------------------- COPY FROM GUEST TO HOST --------------------------------*/
if(!strncmp(cmd,"COPY L",6)){
parse(cmd);
if(!seperate(cmd,from,to,' ')){
report("ERROR- COPY command syntax");
return 1;}
for(i=0;i<strlen(from);i++) /* remove H and L designations */
from[i]=from[i+1];
for(i=0;i<strlen(to);i++)
to[i]=to[i+1];
strcpy(temp,from); /* set up SFR string */
strcat(temp,"|");
strcat(temp,to);
return send_file_request(temp,1);}
/*--------------------------------------------------------------------------------*/
/*-------------------------DELETE FILES-------------------------------------------*/
if(!strncmp(cmd,"DELETE",6)){
parse(cmd);
if(cmd[0]=='H'){
for(i=0;i<strlen(cmd);i++)
cmd[i]=cmd[i+1]; /* remove the first character (H) */
{if(!scb(DF,strlen(cmd)+1,cmd)){
report("ERROR- Host unable to verify DF transmission");
return 1;}
else return passive();}}
if(cmd[0]=='L'){
for(i=0;i<strlen(cmd);i++)
cmd[i]=cmd[i+1]; /* remove the first character (H) */
delete_files(cmd,GUEST);
return 0;}}
/*--------------------------------------------------------------------------------*/
report("ERROR- Command syntax");
return 1;
}
void report(message)
char *message;
{
FILE *fp;
if(SCREENFLAG) cprintf("%s\n\r",message);
if(!LOGFILEFLAG) return;
if((fp=fopen(LOGFILE,"a"))==NULL){
if((fp=fopen(LOGFILE,"r"))==NULL) return;}
fprintf(fp,"%s\n\r",message);
fclose(fp);
}
int init_modem(void)
{
int x;
if(P) a_close(P,0);
P=a_open(COMM,BAUD,0,8,1,8000,2000); /* open at no parity,8 dbits, 1 sbit 8K input ,2k output buffer */
a_setmcr(P,MCR_DTR+MCR_RTS); /* set DTR and RTS high */
a_iflush(P); /* clear input buffer